package com.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

/**
 * @Author: chihaojie
 * @Date: 2019/6/10 21:32
 * @Version 1.0
 * @Note
 */

@Slf4j
@Repository
public class AliMessageSender {


 private static    DefaultProfile profile = DefaultProfile
            .getProfile(
                    "cn-hangzhou",
                    "LTAIKb1PhNoEKZBJ",
                    "KvIOY2GZjiJyz3CFgxFZ6EihO8q3Mz"
            );

   private IAcsClient client=null;

    public AliMessageSender() {
         client = new DefaultAcsClient(profile);
    }

    public Boolean  sendVerCode(String phoneNum,String code){
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("SignName","autohospital");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("TemplateCode","SMS_167396788");
        JSONObject param = new JSONObject();
        param.put("code",code);
        request.putQueryParameter("TemplateParam",param.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("发送短信验证码的结果: {}",JSONObject.toJSON(response));
            return true;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return  false;
    }
}
